package com.yun.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yun.entity.Folder;
import com.yun.entity.User;
import com.yun.service.FolderService;
import com.yun.utils.FileUtil;
import com.yun.utils.ZipUtils;

import net.sf.json.JSONArray;

@Controller
public class FolderController {
	@Resource
	private FolderService folderServiceImpl;

	// 添加文件夹
	@RequestMapping(value = "/addFolder", method = RequestMethod.POST)

	public String addFolder(String foleName, HttpSession session, HttpServletRequest request) {

		// System.out.println("foleName"+foleName);
		String path = request.getServletContext().getRealPath("");
		System.out.println("path:" + path);
		User user = (User) session.getAttribute("existUser");
		int userId = user.getUserId();
		// System.out.println("userId"+userId);
		Folder folder = new Folder(foleName, userId);
		folderServiceImpl.addFolder(folder);
		File file = new File(path + foleName);
		if (!file.exists()) {// 如果文件夹不存在
			file.mkdir();// 创建文件夹
			System.out.println("create");
		}
		// System.out.println("folder="+folder);
		return "admin";

	}

	// 显示文件夹
	@RequestMapping(value = "/folderList")
	public ModelAndView folderList(HttpSession session) {
		List<Folder> getFolderById = new ArrayList<Folder>();
		User user = (User) session.getAttribute("existUser");
		int userId = user.getUserId();
		getFolderById = folderServiceImpl.getFolderById(userId);
		session.setAttribute("folderList", getFolderById);
		// session.setAttribute("getFolderById", getFolderById);
		// System.out.println(getFolderById);
		return new ModelAndView("folder", "getFolderById", getFolderById);
	}

	// 修改文件夹名
	@RequestMapping(value = "/folderEdit")
	public String folderEdit(String foleName, int folderId, HttpSession session, HttpServletRequest request) {
		System.out.println("folderEdit");
		String path = request.getServletContext().getRealPath("");
		User user = (User) session.getAttribute("existUser");
		int userId = user.getUserId();
		boolean flag = folderServiceImpl.editFolder(foleName, folderId, userId);
		File file = new File(path + foleName);
		if (!file.exists()) {// 如果文件夹不存在
			file.mkdir();// 创建文件夹
			System.out.println("update");
		}
		return "forward:/folderList";
	}

	// 移动文件夹
	@RequestMapping(value = "/folderMove")
	public String folderMove(HttpSession session, HttpServletRequest request) {

		int targetId = Integer.parseInt(request.getParameter("targetId"));
		int folderId = Integer.parseInt(request.getParameter("folderId"));

		System.out.println("targetId：" + targetId);
		System.out.println("folderId: " + folderId);

		User user = (User) session.getAttribute("existUser");
		int userId = user.getUserId();

		boolean flag = folderServiceImpl.moveFolder(targetId, folderId, userId);

		return "msg:success";
	}

	// 显示二级文件夹
	@RequestMapping(value = "/folderList2/{folderId}")
	public ModelAndView folderList2(@PathVariable("folderId") Integer folderId, HttpSession session) {
		List<Folder> getFolderById = new ArrayList<Folder>();
		User user = (User) session.getAttribute("existUser");
		int userId = user.getUserId();
		int fatherId = folderId;
		System.out.println("userId:" + userId);
		System.out.println("fatherId:" + fatherId);
		// getFolderById= folderServiceImpl.getFolderById(userId);

		getFolderById = folderServiceImpl.getFolderByFatherId(userId, fatherId);
		// session.setAttribute("getFolderById", getFolderById);
		// System.out.println(getFolderByFtherId);
		return new ModelAndView("folder", "getFolderById", getFolderById);

	}

	// 判断新建文件夹是否存在
	@RequestMapping(value = "/isFolderExist")
	@ResponseBody
	public String isFolderExist(@RequestBody Folder requestFolder) {
		System.out.println("112121");

		System.out.println(requestFolder.getFoleName());
		Folder existFolder = folderServiceImpl.findFolderByName(requestFolder.getFoleName());
		System.out.println("手机号是否存在：" + existFolder);
		if (existFolder == null) {
			return "no";
		} else
			return "ok";

	}

	// 下载文件夹
	@RequestMapping(value = "/downFile")
	@ResponseBody
	public ModelAndView downZip(int folderId, HttpServletRequest request, HttpServletResponse response) throws Exception { // 注意因为我的数组传递过来不是对象形式只是[1,2,3,4]形式，接收的形参用的是String，需要注意，所以不能转为JSONObject
		String path = request.getServletContext().getRealPath("");
		Folder folder = folderServiceImpl.selectByFolderId(folderId);
		// System.out.println("foldername:" + folder.getFoleName());
		ZipUtils.doCompress(path + folder.getFoleName(), path + folder.getFoleName() + ".zip");
		// System.out.println("change world");
		return new ModelAndView("redirect:/folderList");
	}
	// 删除文件夹
	@RequestMapping(value = "/delFolder")
	public String folderDel(@RequestBody Folder folder, HttpSession session,
			HttpServletRequest request) {
		Folder folder1 = folderServiceImpl.selectByFolderId(folder.getFolderId());
		String path = request.getServletContext().getRealPath("");
		File file=new File(path+folder1.getFoleName());
//		System.out.println("aaaaa:"+file.getAbsolutePath());
//		System.out.println("删除文件Id:"+folder.getFolderId());

		User user = (User) session.getAttribute("existUser");
		int userId = user.getUserId();
		boolean flag = folderServiceImpl.delFolder(folder.getFolderId(), userId);
//		System.out.println(flag);
		if (flag==true) {
			boolean f=FileUtil.deleteFile(file);
//			System.out.println("del:"+f);
			return "ok";
		} else {
			return "no";
		}

	}
}
